package fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageButton;

import com.example.filemnager.R;

import java.io.File;
import java.util.List;

import adapter.FileManagerFileListAdapter;
import moudle.FileManagerMoudle;

/**
 * Created by eyesee on 2017/4/25.
 */

public class FileManagerFileList extends Fragment {

    private View mView;
    private ImageButton mButton;
    private GridView mGridView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mView = View.inflate(getActivity(), R.layout.filemanager_filelist, null);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        initView(mView);
        return mView;
    }

    private void initView(View view) {
        //弹出菜单的按钮
        mButton = (ImageButton) view.findViewById(R.id.filelist_bt);
        //显示文件的容器
        mGridView = (GridView) view.findViewById(R.id.filelist_gv);

        setData();
    }

    private void setData() {
        FileManagerMoudle moudle = new FileManagerMoudle();
        List<File> list = moudle.getDataFile();
        FileManagerFileListAdapter adapter = new FileManagerFileListAdapter(getActivity());
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).isDirectory()){
                adapter.addFolder(list.get(i));
            }else if (list.get(i).getName().endsWith(".mp4")){
                adapter.addVideos(list.get(i));
            }
        }
        mGridView.setAdapter(adapter);
    }
}
